package com.zxw.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxw.common.entity.gateway.BlackEntity;
import com.zxw.common.entity.gateway.BlackListQuery;
import com.zxw.common.entity.gateway.BlackQuery;
import com.zxw.common.entity.gateway.BlackVO;
import com.zxw.user.mapper.BlackMapper;
import com.zxw.user.service.BlackService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 黑名单信息表 服务实现类
 * </p>
 *
 * @author zhanxuewei
 * @since 2022-10-10
 */
@Service
public class BlackServiceImpl extends ServiceImpl<BlackMapper, BlackEntity> implements BlackService {


    /**
     * 添加
     *
     * @param blackQuery BlackQuery
     * @return 是否成功 true|false
     */
    @Override
    public boolean save(BlackQuery blackQuery) {
        BlackEntity blackEntity = new BlackEntity();
        BeanUtils.copyProperties(blackQuery, blackEntity);
        blackEntity.setId(null);
        blackEntity.setStatus("1");
        return super.save(blackEntity);
    }

    /**
     * 批量添加
     *
     * @param list List<BlackQuery>
     * @return 是否成功 true|false
     */
    @Override
    public boolean saveBatch(List<BlackQuery> list) {
        List<BlackEntity> collect = list.stream().map(item -> {
            BlackEntity blackEntity = new BlackEntity();
            BeanUtils.copyProperties(item, blackEntity);
            blackEntity.setId(null);
            blackEntity.setStatus("1");
            return blackEntity;
        }).collect(Collectors.toList());
        return super.saveBatch(collect);
    }

    /**
     * 根据ID集合批量删除
     *
     * @param ids id集合
     * @return 是否成功 true|false
     */
    @Override
    public boolean removeByIds(List<String> ids) {
        return super.removeByIds(ids);
    }

    /**
     * 根据ID修改
     *
     * @param blackQuery BlackQuery
     * @return 是否成功 true|false
     */
    @Override
    public boolean updateById(BlackQuery blackQuery) {
        BlackEntity blackEntity = new BlackEntity();
        BeanUtils.copyProperties(blackQuery, blackEntity);
        return super.updateById(blackEntity);
    }

    /**
     * 根据ID查询数据
     *
     * @param id ID
     * @return BlackDO
     */
    @Override
    public BlackEntity getById(String id) {
        return super.getById(id);
    }

    /**
     * 条件分页查询
     *
     * @param blackListQuery BlackListQuery
     * @return IPage<BlackDO>
     */
    @Override
    public IPage<BlackEntity> list(BlackListQuery blackListQuery) {
        IPage<BlackEntity> pageInfo = new Page<>(blackListQuery.getCurrent(), blackListQuery.getSize());
        QueryWrapper<BlackEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlackEntity::getStatus, 1)
                .orderByDesc(BlackEntity::getCreateTime);
        return super.page(pageInfo, queryWrapper);
    }

    @Override
    public IPage<BlackVO> pageList(BlackListQuery blackListQuery) {
        IPage<BlackEntity> pageInfo = new Page<>(blackListQuery.getCurrent(), blackListQuery.getSize());
        QueryWrapper<BlackEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<BlackEntity> lambda = queryWrapper.lambda();
        if (StringUtils.isNotBlank(blackListQuery.getIp())) {
            lambda.like(BlackEntity::getIp, blackListQuery.getIp());
        }
        if (StringUtils.isNotBlank(blackListQuery.getRemark())) {
            lambda.like(BlackEntity::getRemark, blackListQuery.getRemark());
        }
        lambda.orderByDesc(BlackEntity::getCreateTime);
        IPage<BlackEntity> page = super.page(pageInfo, queryWrapper);
        return page.convert(blackEntity -> {
            BlackVO blackVO = new BlackVO();
            BeanUtils.copyProperties(blackEntity, blackVO);
            return blackVO;
        });
    }

    @Override
    public List<BlackVO> queryList() {
        List<BlackEntity> list = super.list();
        if (CollUtil.isNotEmpty(list)) {
            return list.stream().map(e -> {
                BlackVO blackVO = new BlackVO();
                BeanUtils.copyProperties(e, blackVO);
                return blackVO;
            }).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

}
